package com.xidian.zhiancloud.controller.workTicket;

import com.xidian.zhiancloud.constant.FireTicketConstant;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.workTicket.FireTicket;
import com.xidian.zhiancloud.service.workTicket.FireService;
import com.xidian.zhiancloud.util.zyf.WordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.util.*;

import static com.xidian.zhiancloud.util.zyf.WordUtils.beanToMap;

/**
 * @Author: Summerless
 * @Date: 2021/5/8 10:00
 */
@RestController
@RequestMapping("/TicketBase")
public class TicketBaseController {
    //统一导出接口
    @Autowired
    private FireService fireService;

    @Autowired
    private OrganizationMapper organizationMapper;

    @GetMapping("/export")
    public void get(HttpServletRequest request, HttpServletResponse response, @RequestParam("workTicket") Integer workTicket,
                    @RequestParam("ticketId") Long ticketId) throws Exception {
        if (workTicket == 1) {                      // 一级动火票
            //查询动火对象
            //转换map
            //加载模版文件
            //导出
            FireTicket fireTicket = fireService.getById(ticketId);
            // 由单位id获取申请单位名称
            Organization organization = organizationMapper.selectById(fireTicket.getApplicantUnitId());
            String applicantUnitName = (organization == null ? "该部门不存在" : organization.getName());
            // 时间格式化
            String expectStartTime = getFormatTime(fireTicket.getExpectStartTime());
            String expectEndTime = getFormatTime(fireTicket.getExpectEndTime());
            String realStartTime = getFormatTime(fireTicket.getRealStartTime());
            String realEndTime = getFormatTime(fireTicket.getRealEndTime());
            String fireAnalyse1Time = getFormatTime(fireTicket.getFireAnalyse1Time());
            String fireAnalyse2Time = getFormatTime(fireTicket.getFireAnalyse2Time());
            String fireAnalyse3Time = getFormatTime(fireTicket.getFireAnalyse3Time());
            String productUnitTime= getFormatTime(fireTicket.getProductUnitTime());
            String safeUnitTime = getFormatTime(fireTicket.getSafeUnitTime());
            String reviewBeforeWorkTime = getFormatTime(fireTicket.getReviewBeforeWorkTime());
            String finishCheck1Time = getFormatTime(fireTicket.getFinishCheck1Time());
            String finishCheck2Time = getFormatTime(fireTicket.getFinishCheck2Time());
            // 动火方式：字符串分割 + 数组选择
            String workTypeStr = getWorkTypeStr(fireTicket.getWorkType());
            //

            Map<String, Object> params = beanToMap(fireTicket);
            // docx的内容
            params.put("applicantUnitName", applicantUnitName);
            params.put("creatorName", fireTicket.getApplicantName());
            params.put("WorkNumber",fireTicket.getWorkNumber());
            params.put("positionAndContent", fireTicket.getPositionAndContent());
            params.put("workType", workTypeStr);
            params.put("expectStartTime", expectStartTime);
            params.put("expectEndTime", expectEndTime);
            params.put("workDirectorSign", fireTicket.getWorkDirectorSign());
            params.put("workerSign", fireTicket.getWorkerSign());
            params.put("fireAnalyse1Time", fireAnalyse1Time);
            params.put("fireAnalyse2Time", fireAnalyse2Time);
            params.put("fireAnalyse3Time", fireAnalyse3Time);
            params.put("fireAnalyse1Name", fireTicket.getFireAnalyse1Name());
            params.put("fireAnalyse2Name", fireTicket.getFireAnalyse2Name());
            params.put("fireAnalyse3Name", fireTicket.getFireAnalyse3Name());
            params.put("fireAnalyse1Data", fireTicket.getFireAnalyse1Data());
            params.put("fireAnalyse2Data", fireTicket.getFireAnalyse2Data());
            params.put("fireAnalyse3Data", fireTicket.getFireAnalyse3Data());
            params.put("fireAnalyse1Sign", fireTicket.getFireAnalyse1Sign());
            params.put("fireAnalyse2Sign", fireTicket.getFireAnalyse2Sign());
            params.put("fireAnalyse3Sign", fireTicket.getFireAnalyse3Sign());
            params.put("relateWorkNum", fireTicket.getRelateWorkNum());
            params.put("danger1", fireTicket.getDanger1());
            params.put("danger1Sign", fireTicket.getDanger1Sign());
            params.put("danger2", fireTicket.getDanger2());
            params.put("danger2Sign", fireTicket.getDanger2Sign());
            params.put("measure1Sign", fireTicket.getMeasure1Sign());
            params.put("measure2Sign", fireTicket.getMeasure2Sign());
            params.put("measure3Sign", fireTicket.getMeasure3Sign());
            params.put("measure4Sign", fireTicket.getMeasure4Sign());
            params.put("measure5Sign", fireTicket.getMeasure5Sign());
            params.put("measure6Sign", fireTicket.getMeasure6Sign());
            params.put("measure7Sign", fireTicket.getMeasure7Sign());
            params.put("measure8Sign", fireTicket.getMeasure8Sign());
            params.put("measure9Sign", fireTicket.getMeasure9Sign());
            params.put("measure10Sign", fireTicket.getMeasure10Sign());
            params.put("measure11Sign", fireTicket.getMeasure11Sign());
            params.put("otherMeasureSign", fireTicket.getOtherMeasureSign());
            params.put("otherMeasureEditorSign", fireTicket.getOtherMeasureEditorSign());
            params.put("applyDirectorSign", fireTicket.getApplyDirectorSign());
            params.put("firstCheckSign", fireTicket.getFirstCheckSign());
            params.put("dependentSuperviseSign", fireTicket.getDependentSuperviseSign());
            params.put("sceneLeadSign", fireTicket.getSceneLeadSign());
            params.put("possessionSafeSign", fireTicket.getPossessionSafeSign());
            params.put("centralizeManageLeaderSign", fireTicket.getCentralizeManageLeaderSign());
            params.put("workSuperviseSign", fireTicket.getWorkSuperviseSign());
            params.put("productUnitOpinion", fireTicket.getProductUnitOpinion());
            params.put("productUnitSign", fireTicket.getProductUnitSign());
            params.put("productUnitTime", productUnitTime);
            params.put("safeUnitOpinion", fireTicket.getSafeUnitOpinion());
            params.put("safeUnitSign", fireTicket.getSafeUnitSign());
            params.put("safeUnitTime", safeUnitTime);
            params.put("reviewBeforeWorkOpinion", fireTicket.getReviewBeforeWorkOpinion());
            params.put("reviewBeforeWorkSign", fireTicket.getReviewBeforeWorkSign());
            params.put("reviewBeforeWorkTime",reviewBeforeWorkTime);
            params.put("finishCheck1Opinion", fireTicket.getFinishCheck1Opinion());
            params.put("finishCheck1Sign", fireTicket.getFinishCheck1Sign());
            params.put("finishCheck1Time", finishCheck1Time);
            params.put("finishCheck2Opinion", fireTicket.getFinishCheck2Opinion());
            params.put("finishCheck2Sign", fireTicket.getFinishCheck2Sign());
            params.put("finishCheck2Time", finishCheck2Time);
            params.put("realStartTime", realStartTime);
            params.put("realEndTime", realEndTime);


            String path = "workTicket/一级动火票.docx";
            ClassPathResource cpr = new ClassPathResource(path);
            System.out.println(cpr.getPath());
            //这里是我说的一行代码
            //"/Users/zhangyifan/IdeaProjects/zhiancloud/src/main/resources/workTicket/export.docx"
            String fileName = UUID.randomUUID().toString() + ".docx";
            WordUtils.exportWord(cpr.getPath(), fileName, params, request, response);
            WordUtils.delFileWord(fileName);
        }
    }

    /**
     * 获取中文的格式化时间
     *
     * @param originalTime
     * @return
     */
    public String getFormatTime(Date originalTime) {
        // 获取不同格式化风格和中国环境的日期
        DateFormat transTime = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
        // 将不同格式化风格的时间格式化为时间字符串
        String time = transTime.format(originalTime);
        return time;
    }

    /**
     * 返回字符串分割
     *
     * @param originalStr
     * @return
     */
    public List<String> getSplitList(String originalStr) {
        List<String> riskList = new LinkedList<>();
        if (originalStr != null) {
            for(String original : originalStr.split(",")) {
                riskList.add(original);
            }
        }
        return riskList;
    }

    /**
     * 返回动火类型Str
     *
     * @param workType
     * @return
     */
    public String getWorkTypeStr(String workType) {
        String workTypeStr = "";
        List<String> workTypeList = getSplitList(workType);
        for (int i = 0; i < workTypeList.size(); i++) {
            if (workTypeList.get(i) != null && workTypeList.get(i)!="0") {                        // 添加保存为1为风险内容
                workTypeStr = workTypeStr + " " + FireTicketConstant.workTypeStr.get(i);
            }
        }
        return workTypeStr;
    }


}
